package com.itheima.interceptor;

import com.alibaba.fastjson.JSON;
import com.itheima.reggie.common.R;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginCheckInterceptor implements HandlerInterceptor {
    /**
     * 拦截资源前
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
       //获取登录用户
        Long empId = (Long) request.getSession().getAttribute("employee");
        if (empId == null){
            //没有 -- 不放行且响应给用户跳转到登录界面
            response.getWriter().write(JSON.toJSONString(R.error("NOTLOGIN")));
            return false;
        }
        //有 放行
        return true;
    }
}
